<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Index extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'index';         
        $this->load->model('Mindex');
    }
    
    public function index()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', 'Tiêu đề', 'required');
        $this->form_validation->set_rules('content', 'Nội dung', 'required');
        
        $this->data['selectList'] = $this->Mindex->getSelectList();
        
        if($this->form_validation->run() === TRUE)
        {
            $this->data['result'] = $this->Mindex->sendmail();
        }
        $this->data['action'] = __FUNCTION__;
        $this->load->view('temp/layout', $this->data);
    }
    
    public function uploadimg()
    {
        $dataimage = $this->getParamString('data');
        if(!in_array($dataimage, array(null, '', '0')))
        {
            $timenow = time().rand(0,9999);
            $nameimagess = 'nhatnv-'.$timenow.'.png';
            $paths = PROJECT_PATH.'/public/upload/images/';
            $exp = explode("[removed]",$dataimage);
            $this->savefile($exp[1],$paths,$nameimagess);
            $img = 'public/upload/images/'.$nameimagess;
            die($this->data['baseurl'].$img);
        }else{
            die($this->data['baseurl'].'public/upload/images/default.png');
            }
    }
    
    public function listmail()
    {
        $this->data['id_active'] = $this->getParamUri(3,2);
        $this->load->library('pagination');
        $config['per_page'] = 20;
        $this->data['page'] = $this->data['pageg'] = $this->getParamUri(3,2);
        $data = $this->Mindex->getEmailDB($config['per_page'],$this->getParamUri(3,2));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,2);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        
        $this->data['data'] = $data['data'];
        
        $this->data['action'] = 'listmail';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deletemenu()
    {
        $run = $this->Mindex->deleteemail();
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa email thành công");window.location.href="'.$this->data['baseurl'].'manage/email.html";</script>');
        }
        
    }
    
}
